home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / comm / bbs / cit_src_7H21.lha / libtabl.c < prev    next >
C/C++ Source or Header  |  1997-07-27  |  9KB  |  366 lines

  1. /*
  2. *                               libtabl.c
  3. *
  4. * Code to handle CTDLTABL.SYS
  5. */
  6. /*
  7. *                               history
  8. *
  9. * 87Jan20 HAW  Integrity stuff for portability.
  10. * 86Apr24 HAW  Modified for fwrite() and fread().
  11. * 85Nov15 HAW  Created.
  12. */
  13. #include "ctdl.h"
  14. /*
  15. *                               Contents
  16. *
  17. *       readSysTab()            restores system state from ctdltabl.sys
  18. *       common_read()           bottleneck for reading
  19. *       writeSysTab()           saves state of system in CTDLTABL.SYS
  20. *       GetDynamic()            allocation bottleneck
  21. */
  22. CONFIG          cfg;                    /* A buncha variables   */
  23. LogTable        *logTab;                /* RAM index of pippuls */
  24. NetTable        *netTab;                /* RAM index of nodes   */
  25. rTable          *roomTab;               /* RAM index of rooms   */
  26. EVENT           *EventTab = NULL;
  27. char            *indexTable = "ctdlTabl.sys";
  28. struct floor    *FloorTab;
  29. int             TopFloor;
  30. static char     *msg1 = "?old ctdlTabl.sys!";
  31. static struct
  32.   {
  33.   int checkMark;                        /* rudimentary integrity */
  34.   int cfgSize;                  /* sizeof cfg   */
  35.   int logTSize;                 /* logtab size  */
  36.   int endMark;                  /* another integrity check      */
  37.  
  38.   }
  39. integrity;
  40. extern char *R_W_ANY;
  41. static void *FindServes(char *name, char *target);
  42. SListBase Serves =
  43.   {
  44.   NULL, FindServes, NULL, free, NULL
  45.  
  46.   };
  47. /* These two should change from major release to */
  48. #define CHKM    7       /* major release        */
  49. #define ENDM    8
  50. /*
  51. * readSysTab()
  52. *
  53. * This function restores the state of system from CTDLTABL.SYS
  54. * returns:              TRUE on success, else FALSE
  55. * destroys CTDLTABL.TAB after read, to prevent erroneous re-use
  56. * in the event of a crash.
  57. *
  58. * MS-DOS fun: Here's the map --
  59. * Word 1 == sizeof cfg
  60. * Word 2 == sizeof logTab
  61. * Word 3 == sizeof roomTab
  62. * Word 4 -- thru x == cfg contents
  63. * x -- y == logTab
  64. * y -- z == roomTab
  65. * z -- a == netTab
  66. * EOF
  67. */
  68. char readSysTab(char kill, char showMsg)
  69.   {
  70.   FILE  *fd;
  71.   extern char *READ_ANY;
  72.   int           rover;
  73.   long  bytes;
  74.   SYS_FILE    name;
  75.   char  caller;
  76.   label       temp;
  77.   caller = cfg.weAre;
  78.   if ((fd = fopen(indexTable, READ_ANY)) == NULL)
  79.     {
  80.     if (showMsg) printf("?no %s!", indexTable);    /* Tsk, tsk! */
  81.     return(FALSE);
  82.  
  83.     }
  84.   if (fread(&integrity, sizeof integrity, 1, fd) != 1)
  85.     {
  86.     if (showMsg) printf(msg1);
  87.     return FALSE;
  88.  
  89.     }
  90.   if (     integrity.checkMark != CHKM ||
  91.   integrity.endMark != ENDM ||
  92.   integrity.cfgSize != sizeof cfg)
  93.     {
  94.     if (showMsg) printf(msg1);
  95.     return(FALSE);
  96.  
  97.     }
  98.   if (!common_read(&cfg, (sizeof cfg), 1, fd, showMsg))  return FALSE;
  99.   /* Allocations for dynamic parameters */
  100.   logTab = (LogTable *) GetDynamic(integrity.logTSize);
  101.   roomTab = (rTable *) GetDynamic(MAXROOMS * (sizeof (*roomTab)));
  102.   if (cfg.netSize)
  103.   netTab = (NetTable *) GetDynamic(sizeof (*netTab) * cfg.netSize);
  104.   else
  105.   netTab = NULL;
  106.   if (cfg.EvNumber) EventTab  = (EVENT *)GetDynamic(sizeof (*EventTab) * cfg.EvNumber);
  107.   /* "- 1" is kludge */
  108.   if (integrity.logTSize != sizeof (*logTab) * cfg.MAXLOGTAB)
  109.     {
  110.     if (showMsg) printf(msg1);
  111.     return(FALSE);
  112.  
  113.     }
  114.   if (!common_read(logTab, integrity.logTSize, 1, fd, showMsg))
  115.   return FALSE;
  116.   if (!common_read(roomTab, (sizeof (*roomTab)) * MAXROOMS, 1, fd, showMsg))
  117.   return FALSE;
  118.   if (cfg.netSize)
  119.     {
  120.     for (rover = 0; rover < cfg.netSize; rover++)
  121.       {
  122.       if (!common_read(&netTab[rover], NT_SIZE, 1, fd, showMsg)) return FALSE;
  123.       netTab[rover].netTRooms = (SharedRoom *) GetDynamic(SR_BULK);
  124.       if (!common_read(netTab[rover].netTRooms, SR_BULK, 1, fd, showMsg)) return FALSE;
  125.       };
  126.  
  127.     }
  128.   if (cfg.EvNumber)
  129.     {
  130.     if (!common_read(EventTab, (sizeof(*EventTab) * cfg.EvNumber), 1, fd, showMsg))
  131.     return FALSE;
  132.  
  133.     };
  134.   for (rover = 0; rover < cfg.DomainHandlers; rover++)
  135.     {
  136.     if (!common_read(temp, NAMESIZE, 1, fd, showMsg))
  137.       {
  138.       return FALSE;
  139.  
  140.       }
  141.     AddData(&Serves, strdup(temp), NULL, FALSE);
  142.  
  143.     }
  144.   fclose(fd);
  145.   makeSysName(name, "ctdlflr.sys", &cfg.floorArea);
  146.   if ((fd = fopen(name, R_W_ANY)) == NULL)
  147.     {
  148.     if (caller != CONFIGUR)
  149.       {
  150.       if (showMsg) printf("No floor table!");
  151.       return FALSE;
  152.  
  153.       }
  154.  
  155.     }
  156.   else
  157.     {
  158.     totalBytes(&bytes, fd);
  159.     FloorTab = (struct floor *) GetDynamic((int) bytes);
  160.     if (fread(FloorTab, (int) bytes, 1, fd) != 1)
  161.       {
  162.       if (showMsg) printf("problem reading floor tab");
  163.       fclose(fd);
  164.       if (caller != CONFIGUR) return FALSE;
  165.  
  166.       }
  167.     else
  168.       {
  169.       fclose(fd);
  170.       TopFloor = (int) bytes/sizeof(*FloorTab);
  171.  
  172.       }
  173.  
  174.     }
  175.   if (kill) unlink(indexTable);
  176.   crypte(cfg.sysPassword, sizeof cfg.sysPassword, 0);
  177.   return(TRUE);
  178.  
  179.   }
  180. /*
  181. * common_read()
  182. *
  183. * This function reads in from file the important stuff.
  184. * returns:      TRUE on success, else FALSE
  185. */
  186. int common_read(void *block, int size, int elements, FILE *fd,
  187. char showMsg)
  188.   {
  189.   if (size == 0) return TRUE;
  190.   if (fread(block, size, elements, fd) != 1)
  191.     {
  192.     if (showMsg) printf(msg1);
  193.     return FALSE;
  194.  
  195.     }
  196.   return TRUE;
  197.  
  198.   }
  199. static FILE *fd;
  200. /*
  201. * writeSysTab()
  202. *
  203. * This saves state of system in CTDLTABL.SYS
  204. * returns:      TRUE on success, else ERROR
  205. * See readSysTab() to see what the CTDLTABL.SYS map looks like.
  206. */
  207. static void WriteServers(char *name);
  208. int writeSysTab()
  209.   {
  210.   extern char   *WRITE_ANY;
  211.   int   rover;
  212.   if ((fd = fopen(indexTable, WRITE_ANY)) == NULL)
  213.     {
  214.     printf("writeSysTab: problem writing %s\n",indexTable);
  215.     perror("writeSysTab");
  216.     poserr("writeSysTab");
  217.     return(ERROR);
  218.  
  219.     }
  220.   /* Write out some key stuff so we can detect bizarreness: */
  221.   integrity.checkMark = CHKM;
  222.   integrity.endMark = ENDM;
  223.   integrity.cfgSize = sizeof cfg;
  224.   integrity.logTSize = sizeof (*logTab) * cfg.MAXLOGTAB;
  225.  
  226.   if( 1 != fwrite((char *)&integrity, (sizeof integrity), 1, fd) )
  227.     {
  228.     printf("writeSysTab: problem writing integrity data\n");
  229.     perror("writeSysTab");
  230.     poserr("writeSysTab");
  231.     return(ERROR);
  232.     };
  233.  
  234.   crypte(cfg.sysPassword, sizeof cfg.sysPassword, 0);
  235.   if( 1 != fwrite((char *)&cfg, (sizeof cfg), 1, fd) )
  236.     {
  237.     printf("writeSysTab: problem writing configuration data\n");
  238.     perror("writeSysTab");
  239.     poserr("writeSysTab");
  240.     return(ERROR);
  241.     };
  242.  
  243.   crypte(cfg.sysPassword, sizeof cfg.sysPassword, 0);
  244.   if( 1 != fwrite((char *)logTab, (sizeof(*logTab) * cfg.MAXLOGTAB), 1, fd))
  245.     {
  246.     printf("writeSysTab: problem writing User Log table\n");
  247.     perror("writeSysTab");
  248.     poserr("writeSysTab");
  249.     return(ERROR);
  250.     };
  251.  
  252.   if( 1 != fwrite((char *)roomTab, (sizeof (*roomTab)) * MAXROOMS, 1, fd))
  253.     {
  254.     printf("writeSysTab: problem writing room table\n");
  255.     perror("writeSysTab");
  256.     poserr("writeSysTab");
  257.     return(ERROR);
  258.     };
  259.  
  260.    for (rover = 0; rover < cfg.netSize; rover++)
  261.     {
  262.     if( 1 != fwrite((char *)&netTab[rover], NT_SIZE, 1, fd))
  263.       {
  264.       printf("writeSysTab: problem writing Net Table[%d]\n",rover);
  265.       perror("writeSysTab");
  266.       poserr("writeSysTab");
  267.       return(ERROR);
  268.       };
  269.  
  270.     if( 1 != fwrite((char *)netTab[rover].netTRooms, SR_BULK, 1, fd))
  271.       {
  272.       printf("writeSysTab: problem writing Net Table/Room[%d]\n",rover);
  273.       perror("writeSysTab");
  274.       poserr("writeSysTab");
  275.       return(ERROR);
  276.       };
  277.  
  278.     };
  279.   if (cfg.EvNumber)
  280.   if( 1 != fwrite((char *)EventTab, (sizeof(*EventTab) * cfg.EvNumber), 1, fd))
  281.     {
  282.     printf("writeSysTab: problem writing Event Table\n");
  283.     perror("writeSysTab");
  284.     poserr("writeSysTab");
  285.     return(ERROR);
  286.     };
  287.  
  288.   RunList(&Serves, WriteServers);
  289.   fclose(fd);
  290.   return(TRUE);
  291.  
  292.   }
  293. /*
  294. * WriteServers()
  295. *
  296. * This function writes a domain server out to ctdltabl.sys.  See DOMAINS.C
  297. * for more information on this list.
  298. */
  299. static void WriteServers(char *name)
  300.   {
  301.   if( 1 != fwrite((char *)name, NAMESIZE, 1, fd))
  302.     {
  303.     printf("WriteServers: problem writing server data\n");
  304.     perror("writeServers");
  305.     poserr("writeServers");
  306.     };
  307.   }
  308. /*
  309. * GetDynamic()
  310. *
  311. * This does mallocs with error checking.
  312. */
  313. void *special_GetDynamic(unsigned size, char *file, int line)
  314.   {
  315.   void *temp;
  316.   char msg[80];
  317. /**
  318.   if (cfg.BoolFlags.debug)
  319.     {
  320.     splitF(NULL,"GetDynamic(%04.4x, %s, %d)\n",size,file,line);
  321.     };
  322. **/
  323.   if (size == 0) return NULL; /* Simplify code  */
  324.   temp = calloc(1,size);
  325.   /* printf("Requested %d bytes, received address %p\n", size, temp); */
  326.   if (temp == NULL)
  327.     {
  328.     printf("Request for %u bytes of memory failed.\n", size);
  329.     sprintf(msg, "Asked for %u bytes, unable to get it.\n", size);
  330.     crashout(msg);
  331.     }
  332.   return temp;
  333.  
  334.   }
  335. /*
  336. * openFile()
  337. *
  338. * This opens one of the .sys files.
  339. */
  340. void openFile(char *filename, FILE **fd)
  341.   {
  342.   /* We use fopen here rather than safeopen for link reasons */
  343.   if ((*fd = fopen(filename, R_W_ANY)) == NULL)
  344.     {
  345.     printf("?no %s, cannot open it", filename);
  346.     exit(SYSOP_EXIT);
  347.  
  348.     }
  349.  
  350.   }
  351. /*
  352. * FindServes()
  353. *
  354. * This is a find the server function.  It's used in the list of domain servers
  355. * to allow us to search for a domain server based on the name of the domain.
  356. */
  357. static void *FindServes(char *name, char *target)
  358.   {
  359.   if (cfg.BoolFlags.debug)
  360.     {
  361.     splitF(NULL,"FindServes( %s, %s)\n",name, target);
  362.     };
  363.   return (strCmpU(name, target) == SAMESTRING) ? name : NULL;
  364.  
  365.   }
  366.